/*
 * Class PlayerCombat
 *
 * Version 1.0
 *
 * Thursday, August 28, 2008
 *
 * Created by Palidino76 // Updated by ROYALE.
 */

package palidino76.rs2.players.combat;

import palidino76.rs2.Engine;
import palidino76.rs2.players.Player;
import palidino76.rs2.players.items.PlayerWeapon;
import palidino76.rs2.players.*;
import palidino76.rs2.util.Misc;

public class PlayerCombat {
    public void attackPlayer(Player p) {
        if (p.attackPlayer <= 0 || p.isDead || p.attackPlayer >= Engine.players.length || Engine.players[p.attackPlayer] == null || Engine.players[p.attackPlayer].isDead) {
            resetAttack(p);
        }
        if (p.combatDelay > 0) {
            return;
        }
	  if (p.usingSpecial == false) {
	      p.frames.setConfig(p, 301, 0); 
	  }
        Player p2 = Engine.players[p.attackPlayer];
        int hitDamage = Misc.random(maxMeleeHit(p));
	  if (p.canAttackPlayer(p)) {
        	if (p2.absX + 1 == p.absX && p2.absY == p.absY || p2.absX - 1 == p.absX && p2.absY == p.absY || p2.absX == p.absX && p2.absY + 1 == p.absY || p2.absX == p.absX && p2.absY - 1  == p.absY || p2.absX - 1 == p.absX && p2.absY - 1  == p.absY || p2.absX - 1 == p.absX && p2.absY + 1  == p.absY || p2.absX + 1 == p.absX && p2.absY + 1  == p.absY || p2.absX + 1 == p.absX && p2.absY - 1  == p.absY) {
            	if (!p.usingSpecial) {
            	      p.requestAnim(p.attackEmote, 0);
            	} else if (p.usingSpecial) {
		      	if (p.equipment[3] == 11694 && p.specialAmount >= 50) { // Armadyl godsword.
			      	hitDamage = Misc.random((int)(maxMeleeHit(p )*1.25));
					p.usingSpecial = false;
			      	p.specialAmount -= 50;
					p.requestAnim(7074, 0);
	    				p.requestGFX(1222, 0);
	  			} else if (p.equipment[3] == 11696 && p.specialAmount == 100) { // Bandos godsword.
					hitDamage = Misc.random((int)(maxMeleeHit(p )*1.1));
					p.usingSpecial = false;
					p.specialAmount -= 100;
					p.requestAnim(7073, 0);
	    				p.requestGFX(1223, 0);
	  			} else if (p.equipment[3] == 11698 && p.specialAmount >= 75) { // Saradomin godsword.
					hitDamage = Misc.random((int)(maxMeleeHit(p )*1.1));
					p.usingSpecial = false;
					p.specialAmount -= 75;
					p.requestAnim(7071, 0);
	    				p.requestGFX(1220, 0);
	  			} else if (p.equipment[3] == 11700 && p.specialAmount >= 75) { // Zamorak godsword.
					hitDamage = Misc.random((int)(maxMeleeHit(p )*1.1));
					p.usingSpecial = false;
					p.specialAmount -= 50;
					p.requestAnim(7070, 0);
	    				p.requestGFX(1221, 0);
	  			} else if (p.equipment[3] == 4151 && p.specialAmount >= 50) { // Abyssal whip.
					hitDamage = Misc.random((int)(maxMeleeHit(p )*0.9));
					p.usingSpecial = false;
					p.specialAmount -= 50;
					p.requestAnim(1658, 0);
	    				p2.requestGFX(341, 100);
	  			} else if (p.equipment[3] == 1305 && p.specialAmount >= 25) { // Dragon longsword.
					hitDamage = Misc.random((int)(maxMeleeHit(p )*1.0));
					p.usingSpecial = false;
					p.specialAmount -= 25;
					p.requestAnim(1058, 0);
	    				p.requestGFX(248, 100);
					p.requestAnim(1658, 0);
	  			} else if (p.equipment[3] == 4587 && p.specialAmount >= 70) { // Dragon scimitar.
					hitDamage = Misc.random((int)(maxMeleeHit(p )*1.0));
					p.usingSpecial = false;
					p.specialAmount -= 70;
					p.requestAnim(2081, 0);
	    				p.requestGFX(347, 100);
	  			} else if (p.equipment[3] == 1434 && p.specialAmount >= 40) { // Dragon mace.
					hitDamage = Misc.random((int)(maxMeleeHit(p )*1.1));
					p.usingSpecial = false;
					p.specialAmount -= 40;
					p.requestAnim(1060, 0);
	    				p.requestGFX(251, 100);
	  			} else if (p.equipment[3] == 3204 && p.specialAmount >= 100) { // Dragon halberd.
					hitDamage = Misc.random((int)(maxMeleeHit(p )*1.1));
					p.usingSpecial = false;
					p.specialAmount -= 100;
					p.requestAnim(1665, 0);
	    				p.requestGFX(282, 100);
        				p2.appendHit(hitDamage, 0);
	  			} else if (p.equipment[3] == 5698 && p.specialAmount >= 25) { // Dragon dagger(s).
					hitDamage = Misc.random((int)(maxMeleeHit(p )*1.00));
					p.usingSpecial = false;
					p.specialAmount -= 25;
					p.requestAnim(1062, 0);
	    				p.requestGFX(252, 100);
        				p2.appendHit(hitDamage, 0);
        			} else {
            			p.usingSpecial = false;
					p.frames.sendMessage(p, "You don't have enough special energy.");
				}
       		}
        			p.combatDelay = p.attackDelay;
                 		p.requestFaceTo(p2.playerId + 32768);
        			p2.appendHit(hitDamage, 0);
	  			p2.requestAnim(424, 0);
				p.specialAmountUpdateReq = true;
		     		if (p2.autoRetaliate == 0 && !p2.attackingPlayer) {
        				p2.combatDelay += 3;
                 			p2.requestFaceTo(p.playerId + 32768);
		     			p2.attackPlayer = p.playerId;
        	     			p2.attackingPlayer = true;
		      	}
        		} else if (p2.absX == p.absX && p2.absY == p.absY) {
				p.reqWalkQueue(p.absX, p.absY - 1);
      	  	} else if (p2.disconnected[1]) {
				resetAttack(p);
			}
		} else if(!Engine.wildernessArea(p.absX, p.absY)) {
			p.frames.sendMessage(p, "You are not in the wild.");
			resetAttack(p);
		} else if(!Engine.wildernessArea(p2.absX, p2.absY)) {
			p.frames.sendMessage(p, "This player is not in the wild.");
			resetAttack(p);
		} else {
			resetAttack(p);
		}
	}


    /**
     * Read the max hit from your strength level,
     * and your strength bonus.
     */
    public int maxMeleeHit(Player p) {
		int a = p.skillLvl[2];
		int b = p.equipmentBonus[10];
		double c = (double)a;
		double d = (double)b;
		double f = 0;
		double h = 0;
		f = (d*0.00175)+0.1;
		h = Math.floor(c*f+2.05);
		return (int) h;
	}
    public void resetAttack(Player p) {
        if (p == null) {
            return;
        }
        p.attackingPlayer = false;
        if (p.faceToReq != 65535) {
            p.requestFaceTo(65535);
        }
    }
}